package com.ly.web.controller.king;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.erp.KingConstUtil;
import com.ly.heart.domain.BaseRemoteBo;
import com.ly.heart.domain.KingOrgEntity;
import com.ly.heart.service.IKingOrgService;
import com.ly.heart.service.ISupplierService;
import com.ly.heart.util.CurrentUserUtil;
import com.ly.heart.util.KingCommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api (value = "SupplierRemote", tags = "基础数据-供应商")
@RestController
@RequestMapping ("/heart/SupplierRemote")
@Slf4j
public class SupplierRemote {

    @Resource
    private ISupplierService supplierService;
    @Resource
    private IKingOrgService iKingOrgService;

    @ApiOperation ("拉取供应商")
    @PostMapping("/pull")
    public AjaxResult pullSupplier () {
        KingOrgEntity orgEntity = iKingOrgService.getOne ( new LambdaQueryWrapper<KingOrgEntity> ().eq ( KingOrgEntity :: getOrgId, CurrentUserUtil.getCompanyId () ).eq ( KingOrgEntity :: getIsDisabled, false ) );
        if ( orgEntity == null ) {
            throw new CustomException ( "请先至系统管理中配置金蝶账套！" );
        }
        IdentifyInfo identifyInfo = KingCommonUtil.getIdentifyInfo ( orgEntity );
        K3CloudApi client = new K3CloudApi ( identifyInfo );
        BaseRemoteBo baseRemoteBo = new BaseRemoteBo();
        baseRemoteBo.setFormId(KingConstUtil.BD_Supplier);
        List<String> FieldKeys = new ArrayList<>();
        FieldKeys.add("FNumber");
        FieldKeys.add("FName");
        FieldKeys.add("FShortName");
        FieldKeys.add("FGroup");
        FieldKeys.add("FDescription");
//        FieldKeys.add("FBaseInfo");
//        FieldKeys.add("FBusinessInfo");
//        FieldKeys.add("FFinanceInfo");
        FieldKeys.add("FAddress");
        FieldKeys.add("FZip");

        FieldKeys.add("FPayCurrencyId");
        FieldKeys.add("FPayCondition");
        FieldKeys.add("FBankCode");
        FieldKeys.add("FLocName");
        FieldKeys.add("FLocAddress");
        String result = String.join(",", FieldKeys);
        baseRemoteBo.setFieldKeys(result);
        String jsonData = JSON.toJSONString(baseRemoteBo);
        try {
            //调用接口
            List<List<Object>> lists = client.executeBillQuery ( jsonData );
            System.out.println("lists："+lists);
            for ( int i = 0; i < lists.size (); i++ ) {
                List<Object> objects = lists.get ( i );

            }
        } catch ( Exception e ) {
            throw new RuntimeException ( "同步供应商失败！" );
        }
        return AjaxResult.success ( "同步供应商成功！" );
    }





}
